package com.easy.service.impl;

import java.util.List;
import java.util.Map;

import com.easy.bean.Notice;
import com.easy.dao.NoticeDao;
import com.easy.service.Service;

public class NoticeService implements Service{
private NoticeDao noticeD=new NoticeDao();
	
	public List<Notice> queryList(Map params){
		String checktext=(String)params.get("checktext");
		String selecttext=(String)params.get("selecttext");
		String sql="select * from h_notice where 1=1 ";
		try {
			if((!selecttext.equals(null)) && (!checktext.equals(null))) {
			sql+="and " + selecttext + " like " + "'%"+checktext+"%' ";
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(params.get("limit")!=null) {
			int limit=Integer.parseInt((String)params.get("limit"));
			int page=Integer.parseInt((String)params.get("page"));
			int start=(page-1)*limit;
			sql+="limit "+start+","+limit;
		}
		return noticeD.querList(sql);
	}
	
	public Notice query(String n_id) {
		String sql = "select n_id,n_context,n_title from h_notice where n_id='" + n_id+"'";
		return noticeD.query(sql);
	}

	@Override
	public Object query(Map params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int queryCount(Map params) {
		String checktext=(String) params.get("checktext");
		String sql = "select count(*) from h_notice where 1=1";
		if(checktext != null) {
			sql+=" and n_id like '%"+checktext+"%' ";
			sql+=" or n_context like '%"+checktext+"%' ";
			sql+=" or n_title like '%"+checktext+"%' ";
		}
		return noticeD.queryCount(sql);
	}

	@Override
	public int update(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("update h_notice set ");
		strB.append("n_id='");
		strB.append(params.get("n_id"));
		strB.append("',u_id='");
		strB.append(params.get("u_id"));
		strB.append("',n_date='");
		strB.append(params.get("n_date"));
		strB.append("',n_readnum='");
		strB.append(params.get("n_readnum"));
		strB.append("',n_context='");
		strB.append(params.get("n_context"));
		strB.append("',n_title='");
		strB.append(params.get("n_title"));
		strB.append("' where n_id='");
		strB.append(params.get("n_id"));
		strB.append("'");
		int count = noticeD.update(strB.toString());
		return count;
	}

	@Override
	public int delete(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("delete from h_notice where n_id='");
		strB.append(params.get("n_id"));
		strB.append("'");
		int count = noticeD.delete(strB.toString());
		return count;
	}

	@Override
	public int insert(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("insert into h_notice(n_id,u_id,n_date,n_readnum,n_context,n_title) values(");
		strB.append("'");
		strB.append(params.get("n_id"));
		strB.append("','");
		strB.append(params.get("u_id"));
		strB.append("','");
		strB.append(params.get("n_date"));
		strB.append("','");
		strB.append(params.get("n_readnum"));
		strB.append("','");
		strB.append(params.get("n_context"));
		strB.append("','");
		strB.append(params.get("n_title"));
		strB.append("'");
		strB.append(")");
		System.out.println(strB.toString());
		int count = noticeD.insert(strB.toString());
		return count;
	}
	
	public int updateread(String n_id) {
		String sql="update h_notice set n_readnum=n_readnum+1 where n_id="+n_id;
		System.out.println(sql);
		return noticeD.update(sql);
	}
}
